//
//  GWRhythmView.m
//  Glowalker
//
//  Created by Harvey on 2017/10/5.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "GWRhythmView.h"

@implementation GWRhythmView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (instancetype)initWithBlock:(void (^)(UISwitch *sender))block
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        _switchBlock = block;
        [self initSubViews];
    }
    
    return self;
}

- (void)initSubViews
{
    UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    [self addSubview:coverView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgClick)];
    [coverView addGestureRecognizer:tapGesture];
    
    UIView *containView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight-80, kWidth, 80)];
    containView.backgroundColor = [UIColor whiteColor];
    [coverView addSubview:containView];
    
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kWidth, 0.5)];
    lineV.backgroundColor = [UIColor grayColor];
    [containView addSubview:lineV];
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 40, kWidth, 40)];
    [closeBtn setTitle:showText(@"关闭") forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [containView addSubview:closeBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 40)];
    titleLabel.text = showText(@"音乐律动");
    [containView addSubview:titleLabel];
    
    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(kWidth-60, 5, 50, 30)];
    NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
    switchView.on = [userdefaults boolForKey:kRhythmMode];
    [switchView addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
    [containView addSubview:switchView];
}

- (void)switchChange:(UISwitch *)sender
{
    if (_switchBlock) {
        _switchBlock(sender);
    }
}

- (void)bgClick
{
    [self hide];
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)hide
{
    [self removeFromSuperview];
}

@end
